package com.briup.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.ICustomerServiceImpl;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet{
    private ICustomerService customerService = new ICustomerServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String zipCode = req.getParameter("zipCode");
        String address = req.getParameter("address");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email");

        //2. 封装对象
        Customer customer = new Customer(username, password, zipCode, address, telephone, email);
        //2.1调用service 接口 -实现类对象
        //3注册成功--登录
        // 注册失败--注册页面(用户名已经存在 用户不存在【  】)
        String path = "/login.jsp";
        String msg = "注册成功，请登录";
        try {
            customerService.register(customer);
        } catch (Exception e) {
            e.printStackTrace();
            path ="/register.jsp";
            msg = "注册失败,"+e.getMessage();
        }
        req.setAttribute("msg", msg);
        req.getRequestDispatcher(path).forward(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //127.0.0.1:9999/estore_2110/register---get
        //注册功能---请你访问register.jsp---post
        //服务器内部跳转 --重定向
        req.getRequestDispatcher("/register.jsp").forward(req, resp);
    }

}
